var db = require('../util/DB.js');    
var co = require('co'); 

module.exports =  {

    insert: function(param) {
        return new Promise(function(resolve, reject){
            console.log(param);
            co(function *  (){
                var sqlValue = {
                    user_name:  param.user_name,
                    user_id:    param.user_id,
                    email:      param.email,
                    password:   param.password,
                    phone_num:  param.phone_num ? param.phone_num : 0,
                    status: param.status ? param.status: 0,
                    create_time: (new Date()).toLocaleString(),
                    update_time: (new Date()).toLocaleString(),
                };
                var sql =  db.format('insert into user set ?', sqlValue);
                  
                console.log(sql);
                yield db.query(sql).then(function(res){
                    resolve(res);
                }).catch(function(err){
                    reject(err);
                });
            });
        })
    },
    update: function(param) {
        return new Promise(function(resolve, reject){
            co(function *  (){
                var sqlValue = {};
                var whereValue = {
                    user_id: param.user_id
                }
                if(param.user_id){
                    delete param.user_id;
                }
                for( var i in param){
                    sqlValue[i] = param[i];
                }

                console.log(sqlValue,whereValue);
                var sql = db.format('update story set ? where ? ' ,  [sqlValue, whereValue]);
                console.log(sql);
                yield db.query(sql).then(function(res){
                    resolve(res);
                }).catch(function(err){
                    reject(err);
                });
            });
        })
    },
    getUserInfoByPrimaryKey: function(param) {
        return new Promise(function(resolve, reject){
            co(function *  (){
                var whereValue = {};
                if(param.user_id){
                    whereValue = {
                        user_id: param.user_id
                    }
                } else if(param.email){
                    whereValue = {
                        email: param.email
                    }
                }
                
                var fields = [];
                var sql = '';
                if(param.fields){
                    fields = param.fields;
                    sql = db.format('select ? from user  where ? ' ,  [fields, whereValue]);
                } else {
                    sql = db.format('select * from user  where ? ' ,  [whereValue]);
                }
               
                
                console.log(sql);
                yield db.query(sql).then(function(res){
                    resolve(res[0]);
                }).catch(function(err){
                    reject(err);
                });
            });
        })
    },
    
    
    
}
